<?php

namespace App\Console;

use App\Console\Commands\CollecterPublishPostsJob;
use App\Console\Commands\GatewayWorkerServer;
use App\Jobs\Bureau\BureauNoticeJob;
use App\Jobs\ChatExpireCleanJob;
use App\Jobs\ExperienceExpireCleanJob;
use App\Jobs\ImageUnusedCleanJob;
use App\Jobs\Order\OrderDeliverInfoManage;
use App\Jobs\OvertimeOrdersDeleteJob;
use App\Jobs\Posts\UpdateCommentOnlyTextJob;
use App\Jobs\Posts\UpdatePostFieldsJob;
use App\Jobs\Posts\UpdatePostFilesIdJob;
use App\Jobs\Posts\UpdatePostGoodsIdJob;
use App\Jobs\Posts\UpdatePostOnlyTextJob;
use App\Jobs\Posts\UpdatePostPopularCommentJob;
use App\Jobs\Posts\UpdatePostSoundsIdJob;
use App\Jobs\Posts\UpdatePostTagIdJob;
use App\Jobs\Posts\UpdatePostTenantObjJob;
use App\Jobs\Posts\UpdatePostVideoIdJob;
use App\Jobs\Posts\UpdatePostVoteIdJob;
use App\Jobs\Posts\UpdatePostWeightRefreshJob;
use App\Jobs\PostTypeUpdateJob;
use App\Jobs\pushTaskRewardTipJob;
use App\Jobs\RealtimePostsDegreeJob;
use App\Jobs\SearchEnginePushJob;
use App\Jobs\SitemapUpdateJob;
use App\Jobs\System\TmpMetaClearJob;
use App\Jobs\UpdateAttachmentTypeJob;
use App\Jobs\UpdateCommentCircleJob;
use App\Jobs\UpdatePostImgCountJob;
use App\Jobs\User\UserDeleteAfterJob;
use App\Jobs\User\UserWithdrawalProcessJob;
use App\Jobs\User\UserWithdrawalQueryJob;
use App\Jobs\UserRobotExpIncreaseJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;

class Kernel extends ConsoleKernel
{
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
        GatewayWorkerServer::class
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        global $__MINI_GLOBAL_REQUEST_ENV__;
        $__MINI_GLOBAL_REQUEST_ENV__ = 'console';
        try {
            DB::connection()->getPdo();
        } catch (\Exception $e) {
            _logger_(__file__, __line__, '数据库连接不上了. Error: ' . $e->getMessage());
            return;
        }
        // $schedule->command('inspire')->hourly();
        $schedule->command('autotask:run')->everyMinute();
        $schedule->command('chmod:run')->everySixHours();
        $schedule->job(new ImageUnusedCleanJob)->hourly();
        $schedule->job(new ChatExpireCleanJob)->daily();
        $schedule->job(new SearchEnginePushJob)->everyTwoHours();
        $schedule->job(new SitemapUpdateJob)->everySixHours();
        $schedule->job(new RealtimePostsDegreeJob)->everyFifteenMinutes();
        $schedule->job(new OvertimeOrdersDeleteJob())->everyFiveMinutes();
        $schedule->job(new BureauNoticeJob())->everyFiveMinutes();
        $schedule->job(new ExperienceExpireCleanJob)->hourly();
        $schedule->job(new PostTypeUpdateJob())->everyTenMinutes();

        $schedule->job(new UserWithdrawalProcessJob(0))->everyFifteenMinutes();
        $schedule->job(new UserWithdrawalQueryJob(0))->everyTenMinutes();

        $schedule->job(new UpdateAttachmentTypeJob())->everyThirtyMinutes();
        $schedule->job(new pushTaskRewardTipJob())->everyTenMinutes();
        $schedule->job(new UpdateCommentCircleJob([]))->everyFiveMinutes();
        $schedule->job(new UpdatePostPopularCommentJob())->everyFiveMinutes();
        $schedule->job(new UserRobotExpIncreaseJob())->everyFourHours();
        $schedule->job(new UpdatePostImgCountJob(0,0,0))->everyTenMinutes();
        $schedule->job(new UpdatePostVideoIdJob(0))->everyThirtyMinutes();
        $schedule->job(new UpdatePostTagIdJob(0))->everyThirtyMinutes();
        $schedule->job(new UpdatePostFilesIdJob(0))->everyThirtyMinutes();
        $schedule->job(new UpdatePostGoodsIdJob(0))->everyThirtyMinutes();
        $schedule->job(new UpdatePostSoundsIdJob(0))->everyThirtyMinutes();
        $schedule->job(new UpdatePostVoteIdJob(0))->everyThirtyMinutes();
        $schedule->job(new UpdatePostFieldsJob(0))->everyThirtyMinutes();
        $schedule->job(new UpdatePostOnlyTextJob(0))->everyTenMinutes();
        $schedule->job(new UpdateCommentOnlyTextJob(0))->everyTenMinutes();
        $schedule->job(new UserDeleteAfterJob(0))->everyFourHours();
        $schedule->job(new UpdatePostWeightRefreshJob())->everyTwoMinutes();
        $schedule->job(new OrderDeliverInfoManage('', 0))->everyTenMinutes();
        $schedule->job(new UpdatePostTenantObjJob(0))->everyTenMinutes();
        $schedule->job(new TmpMetaClearJob())->everySixHours();

        $schedule->job(new CollecterPublishPostsJob)->everyTenMinutes();
        Cache::put('schedule', 1, 120);
        $this->queueCheck();
    }

    protected function queueCheck(){

        if(Queue::size() < Cache::get('queue:size:cache', 0)){
            // 清零
            Cache::forget('queue:increment:times');
        }else{
            if(Queue::size() != 0){
                Cache::increment('queue:increment:times');
            }
        }
        Cache::put('queue:size:cache', Queue::size(), 120);
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }

    /**
     * 获取计划事件默认使用的时区
     *
     * @return \DateTimeZone|string|null
     */
    protected function scheduleTimezone()
    {
        return 'Asia/Shanghai';
    }

}
